// #ifdef MP-WEIXIN
import { getSessionCode } from '@/common/wechat';
// #endif


import { member} from '@/api/member';

export default  {
  data () {
	return {
		positionTop: 0,
		isDevtools: false,
		userInfo:{},
		weixinUserKey:{}  ,//微信openID
		member:{},
		host:process.env.HOST,
		params: {
			page: 1,
			pageSize: 20,
		},
	}
  },
  
	methods: {
	  
		initPosition() {
			/**
			 * 使用 absolute 定位，并且设置 bottom 值进行定位。软键盘弹出时，底部会因为窗口变化而被顶上来。
			 * 反向使用 top 进行定位，可以避免此问题。
			 */
			this.positionTop = uni.getSystemInfoSync().windowHeight - 100;
		},

		
		//平台认证
		oauth() {
			var that=this;
			this.$store.dispatch("getCode").then(function(res){
				that.$store.dispatch("getAppToken");   //获取用户token
			}).then(function(){
				return that.$store.dispatch("getAppUser");  //获取平台用户信息
			}).then(function(res){
				//console.log(that.$store.state.user.appUserInfo);
				//uni.reLaunch({url:"/pages/user/info/index"});//认证成功跳转信息完善页面
			}).catch(function(){
				console.log("用户认证失败！")
			});
		},
	
	
		getUserInfo({detail}) {
			if (detail.userInfo) {
				this.oauth();
			} else {
				uni.showToast({icon: 'none',title: '登陆失败'});
			}
		},
		
		
		getUserProfile(detail){
			uni.getUserProfile({
				 desc: '完善个人信息', 
				success: (res) => {
				   console.log(res.userInfo);
				}
			})
		},
		// 取消
		onCancel:function() {},
		

			
		goPage:function(e){
			
			if(!e.currentTarget.dataset.url) return false; 
			
			//uni.reLaunch({url: e.currentTarget.dataset.url});
			
			switch(e.currentTarget.dataset.type){
				case "switch":
					uni.switchTab({
						url: e.currentTarget.dataset.url
					});
				break;
				default:
					uni.navigateTo({
						url: e.currentTarget.dataset.url
					});
				break;
			}
		},
		
		
	
		getDate:function(type) {
			const date = new Date();
			let year = date.getFullYear();
			let month = date.getMonth() + 1;
			let day = date.getDate();
			
			if (type === 'start') {
				year = year - 60;
			} else if (type === 'end') {
				year = year + 2;
			}
			month = month > 9 ? month : '0' + month;;
			day = day > 9 ? day : '0' + day;
			return `${year}-${month}-${day}`;
		},
		
		async initAppInfo() {
			
			//let userInfo = uni.getStorageSync('APP_USER_INFO');
			
			let weixinUserKey = uni.getStorageSync('WEIXIN_USER_KEY');
			if(weixinUserKey) this.member.wechat_info=weixinUserKey;
			
			let userToken =  uni.getStorageSync('USER_TOKEN');
			this.member.token=userToken;//设置用户token信息
		   
			if(userToken){
				let memberInfo =this.$store.state.user.memberInfo
				if(memberInfo){
					this.member=memberInfo;
					uni.setStorageSync('USER_ID',memberInfo["id"]);
				}else{
					let userDetail = await member.getMember({token:userToken});					
					if(userDetail&&userDetail["id"]){
						this.member=userDetail;
						uni.setStorageSync('MEMBER_INFO',userDetail);
						uni.setStorageSync('USER_ID',userDetail["id"]);
						this.$store.commit("setMemberInfo",userDetail);
					}else{
						uni.reLaunch({url:"/pages/user_oauth/index"});
					}	
					
					//uni.reLaunch({url:"/pages/user_oauth/index"});
				}

				if(this.member&&!this.member["account_num"]){
					uni.reLaunch({url:"/pages/user_oauth/index"});
				}
				
			}

			this.initData();//全局数据加载完成后，加载子页面数据

		},
		
		//心跳检测
		async heartBeat(){
			let that=this;
			let intervals =this.$store.state.user.intervals;

			let intervalId = setInterval(async function(){
				await member.heartBeat({});
			},20000);　		
			intervals.push(intervalId);
			this.$store.commit("setIntervals",intervals);
		}
	

	},
	
	onHide(){
		//清除定所有时任务
		let intervals =this.$store.state.user.intervals;
		for(let index in intervals){
			clearInterval(intervals[index]);
		}
		this.$store.commit("setIntervals",[]);
	},
	

	onReady() {  
		this.initPosition();
		this.$store.dispatch("getProvider");//设置平台信息
		// #ifdef MP-WEIXIN
		this.isDevtools = uni.getSystemInfoSync().platform === 'devtools'; //判断是否为开发者工具
		// #endif
		
		let userToken =  uni.getStorageSync('USER_TOKEN');
		if(!userToken) this.oauth();//进行平台认证
		
		
		
	},
	
	
	
	onShow(){
		this.initAppInfo();
		
		let userToken =  uni.getStorageSync('USER_TOKEN');
		if(userToken) this.heartBeat();
	},
	
	
	
 
  
}
